//index.js
//获取应用实例
const app = getApp()
wx.cloud.init();
var lastclick = null;
Page({
  data: {
    hasUserInfo: false,
    buttons: [{text: '取消'}, {text: '确定'}],
  },
 
  loadData: function () {
    var that = this;
    wx.cloud.callFunction({
      // 云函数名称
      name: 'admin',
      // 传给云函数的参数
      data: {
      },
      success: function (res) {
        console.log(res);
      },
      fail: err => {
        console.log(err);
        wx.showToast({
          icon: 'none',
          title: '加载失败，请尝试刷新',
          duration: 2000
        })
      }
    });
  },
  onLoad: function () {
    wx.getSystemInfo().then(function (res) {
      console.log('client platform: ', res.platform);
    });
    var that = this;
    this.loadData();
  },
  gotoUserHome: function () {
    lastclick = 'gotouser';
    if(!this.data.userRole){
      this.setData({
        dialogShow: true
      });
    } else {
      this.continueGoto();
    }
  },
  gotoHostHome: function () {
    lastclick = 'gotohost';
    if(!this.data.userRole){
      this.setData({
        dialogShow: true
      });
    } else {
      this.continueGoto();
    }
  },
  continueGoto: function(e){
    if(e && e.detail.index === 0){
      this.setData({dialogShow: false});
      return;
    } else if(e){
      this.setData({dialogShow: false});
    }
    if(lastclick === 'gotohost'){
      wx.navigateTo({
        url: '../contest/contest?area=host',
      });
    } else if(lastclick === 'gotouser'){
      wx.navigateTo({
        url: '../contest/contest?area=user',
      });
    }
  },
  onShareAppMessage: function (res) {
    return {
      title: '文艺比赛小行家',
      path: '/pages/index/index',
      imageUrl: '../../assets/singer.png'
    }
  },
  showLoginTip: function () {
    this.setData({
      showTip: true
    });
    console.log(this.data);
  },
  hideLoginTip: function () {
    this.setData({
      showTip: false
    });
  },
  getUserInfo: function (e) {
    if (!e.detail.userInfo) {
      return;
    }
    console.log('opendata userinfo', e.detail.userInfo);
    var nickname = e.detail.userInfo.nickName;
    var that = this;
    app.getUserRole(nickname).then(function (role) {
      console.log(role);
      e.detail.userInfo.role = role;
      var userType = app.translateRole(role);
      that.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
        userRole: role,
        userType: userType
      });
      app.setGlobalUser(e.detail.userInfo);
    });
  },
  onShareTimeline: function (res) {
    return {
      title: '文艺比赛小行家',
      path: '/pages/index/index',
      imageUrl: '../../assets/singer.png'
    }
  }

})
